package com.eoner.uikit.header;

import android.animation.Animator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieComposition;
import com.eoner.uikit.R;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

/**
 * <pre>
 *     Copyright    : Copyright (c) 2020.
 *     Authour      : JiaoYa.
 *     Created Time : 2020/4/24.
 *     Desc         :
 *     Alter By     :
 * </pre>
 *
 * @author JiaoYa
 */

public class MyRefreshHead extends ClassicsHeader {

    public static final String LOTTIE = "LOTTIE";
    public static final String PROGRESS = "PROGRESS";

    private View root;
    private View viewProgress;
    private LottieAnimationView lottieAnimationView;
    private TextView tvLoadTxt;
    private Context mContext;
    //0：红色；1：白色
    private int type = 0;
    private String actionType = LOTTIE;

    public MyRefreshHead(Context context) {
        super(context);
//        initView(context);
    }

    public MyRefreshHead(Context context, int type, String actionType) {
        super(context);
        this.type = type;
        this.actionType = actionType;
//        initView(context);
    }

    public MyRefreshHead(Context context, AttributeSet attrs) {
        super(context, attrs);
//        this.initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.loading_lottie, this);
        root = view.findViewById(R.id.root);
        lottieAnimationView = view.findViewById(R.id.lottie);
        viewProgress = view.findViewById(R.id.view_progress);
        tvLoadTxt = view.findViewById(R.id.tv_load_txt);

        if (actionType.equals(LOTTIE)) {
            lottieAnimationView.setVisibility(VISIBLE);
            viewProgress.setVisibility(GONE);
            lottieAnimationView.addAnimatorListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    if (type == 0) {
                        showLocalLottieEffects("data333");
                    } else {
                        showLocalLottieEffects("data33");
                    }
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {
                }
            });
        }else {
            lottieAnimationView.setVisibility(GONE);
            viewProgress.setVisibility(VISIBLE);
        }
    }

    /**
     * 从本地查找lottie动画(切换不同动画防止闪动)
     *
     * @param interactCode 特效name
     */
    private void showLocalLottieEffects(String interactCode) {
        //json文件的路径根据具体需求修改
        @SuppressLint("WrongThread")
        LottieComposition composition = LottieComposition.Factory.fromFileSync(mContext, interactCode + ".json");
        lottieAnimationView.cancelAnimation();
        lottieAnimationView.setProgress(0);
        lottieAnimationView.setComposition(composition);
        lottieAnimationView.playAnimation();
        lottieAnimationView.setVisibility(View.VISIBLE);
    }


    public void changeBackColor(int color) {
//        root.setBackgroundColor(color);
    }

    public void changeTextColor(int color) {
//        tvLoadTxt.setTextColor(color);
    }

//    @Override
//    @NonNull
//    public View getView() {
//        ///真实的视图就是自己，不能返回null
//        return this;
//    }
//
//    @Override
//    public SpinnerStyle getSpinnerStyle() {
//        //指定为平移，不能null
//        return SpinnerStyle.Translate;
//    }
//
//    @Override
//    public void onStartAnimator(RefreshLayout layout, int headHeight, int maxDragHeight) {
//        if (actionType.equals(LOTTIE)) {
////            lottieAnimationView.playAnimation();//开始动画
//        }
//    }
//
////    @Override
////    public int onFinish(RefreshLayout layout, boolean success) {
////        if (actionType.equals(LOTTIE)) {
//////        lottieAnimationView.cancelAnimation();//停止动画
//////        lottieAnimationView.clearAnimation();
////        }
////        if (success) {
////            tvLoadTxt.setText("刷新完成");
////        } else {
////            tvLoadTxt.setText("刷新失败");
////        }
////        //延迟500毫秒之后再弹回
////        return 2000;
////    }
//
//    @Override
//    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
////        switch (newState) {
////            case None:
////            case PullDownToRefresh:
////                tvLoadTxt.setText("松开立即刷新…");
////                if (actionType.equals(LOTTIE)) {
////                    if (type == 0) {
////                        lottieAnimationView.setAnimation("data111.json");
////                    } else {
////                        lottieAnimationView.setAnimation("data11.json");
////                    }
////                }
////                break;
////            case Refreshing:
////                tvLoadTxt.setText("正在加载中，请稍后…");
////                if (actionType.equals(LOTTIE)) {
////                    if (type == 0) {
////                        showLocalLottieEffects("data222");
////                    } else {
////                        showLocalLottieEffects("data22");
////                    }
////                }
////                break;
////            case ReleaseToRefresh:
////                //释放立即刷新
////                break;
////            default:
////                break;
////        }
//    }
//
//    @Override
//    public boolean isSupportHorizontalDrag() {
//        return false;
//    }
//
//    @Override
//    public void onInitialized(RefreshKernel kernel, int height, int maxDragHeight) {
//    }
//
//    @Override
//    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
//
//    }
//
//    @Override
//    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
//
//    }
//
//    @Override
//    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
//    }
//
//    @Override
//    public void setPrimaryColors(@ColorInt int... colors) {
//    }
}
